package org.ndx.jsg.output;

import java.io.File;
import java.util.Collection;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.configuration.Configuration;
import org.ndx.jsg.FileInfos;

import com.google.inject.Inject;

public class DefaultOutputManager implements OutputManager {
	private static Logger logger = Logger.getLogger(DefaultOutputManager.class.getName());
	/**
	 * List of output provider, each will receive the file and handle it the most appropriated way
	 */
	@Inject private Set<OutputProvider> outputProviders;
	
	/**
	 * Before to output anything, output providers are initialized, and they finally are disposed
	 */
	@Override
	public void output(Configuration config, Collection<FileInfos> infos) {
		for(OutputProvider p : outputProviders) {
			p.prepare(config);
		}
		for(FileInfos i : infos) {
			handle(i);
		}
		for(OutputProvider p : outputProviders) {
			p.terminate(config);
		}
	}

	/**
	 * Handle file infos using all appropriated output providers
	 * @param infos file infos
	 */
	private void handle(FileInfos infos) {
		Set<OutputProvider> used = get(infos);
		for(OutputProvider p : used) {
			p.handle(infos);
		}
	}

	/**
	 * Get output providers appropriated to file infos
	 * TODO implement
	 * @param infos
	 * @return
	 */
	private Set<OutputProvider> get(FileInfos infos) {
		return outputProviders;
	}
}
